package com.bootdo.fx.service.impl;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.fx.dao.GoodsCategoryDao;
import com.bootdo.fx.domain.GoodsCategoryDO;
import com.bootdo.fx.service.GoodsCategoryService;



@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {
	@Autowired
	private GoodsCategoryDao goodsCategoryDao;
	
	@Override
	public GoodsCategoryDO get(Long categoryId){
		return goodsCategoryDao.get(categoryId);
	}
	
	@Override
	public List<GoodsCategoryDO> list(Map<String, Object> map){
		return goodsCategoryDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsCategoryDao.count(map);
	}
	
	@Override
	public int save(GoodsCategoryDO goodsCategory){
		return goodsCategoryDao.save(goodsCategory);
	}
	
	@Override
	public int update(GoodsCategoryDO goodsCategory){
		return goodsCategoryDao.update(goodsCategory);
	}
	
	@Override
	public int remove(Long categoryId){
		return goodsCategoryDao.remove(categoryId);
	}
	
	@Override
	public int batchRemove(Long[] categoryIds){
		return goodsCategoryDao.batchRemove(categoryIds);
	}

	@Override
	public Tree<GoodsCategoryDO> getTree() {

			List<Tree<GoodsCategoryDO>> trees = new ArrayList<Tree<GoodsCategoryDO>>();
			List<GoodsCategoryDO> goodsCategoryDOList = goodsCategoryDao.list(new HashMap<String,Object>(16));
			for (GoodsCategoryDO goodsCategoryDO : goodsCategoryDOList) {
				Tree<GoodsCategoryDO> tree = new Tree<GoodsCategoryDO>();
				tree.setId(goodsCategoryDO.getCategoryId());
				tree.setParentId("0");
				tree.setText(goodsCategoryDO.getCategoryName());
				Map<String, Object> state = new HashMap<>(16);
				state.put("opened", true);
				tree.setState(state);
				trees.add(tree);
			}
			// 默认顶级菜单为０，根据数据库实际情况调整
			Tree<GoodsCategoryDO> t = BuildTree.build(trees);
			return t;

	}

}
